﻿using UnityEditor;
using UnityEngine;
using UnityEngine.AddressableAssets;
using UnityEngine.ResourceManagement.AsyncOperations;

namespace GameFramework.common
{
    //TODO excel配置文件转成什么格式来加载？json,xml,protobuf,scriptableobject
    //TODO 最好做到加载速度快，内存小，能只加载用到的，不用的不加载，减少内存
    //TODO 方案1：excel->csv->csv.bytes->用二进制留包装成文本流，按行解析数据并加进内存，需要扩展编辑器自动copy excel文件到csv.bytes文件
    //TODO 方案2：excel->csv->csv.bytes->用二进制留包装成文本流，按行解析数据并加进内存，需要扩展编辑器自动copy excel文件到csv.bytes文件，并
    //利用scirptable文件在编辑器魔术下可以写入的特性，读取数据写入到assets文件
    public class ScriptableObjectSingleton<T>:ScriptableObject where T:ScriptableObject
    {  
        private static T s_Instance;

        // protected ScriptableObjectSingleton()
        // {
        //     init();
        // }

        public static T Instance
        {
            get
            {
                if (s_Instance == null)
                {
                    // string[] findAssets = AssetDatabase.FindAssets($"t:{typeof(T).Name}");
                    AsyncOperationHandle<T> handle = Addressables.LoadAssetAsync<T>($"Assets/AdressableResource/excel_configs/{typeof(T).Name}.asset");
                    T o = handle.WaitForCompletion();
                    (o as ScriptableObjectSingleton<T>).init();
                    s_Instance = o;
                }
                return s_Instance;
            }
        }
        protected virtual void  init()
        {
            
        }
    }
}